package future;

import java.util.concurrent.*;

/**
 * Created by yiyongyao on 2018/3/2.
 */
public class FutureExample {

    public static void main(String[] args) throws ExecutionException, InterruptedException {


        ExecutorService executor = Executors.newCachedThreadPool();

        Runnable task1 = new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("这是任务1...");
            }
        };

        Callable<Integer> task2 = new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                return new Integer(100);
            }
        };

        Future<?> f1= executor.submit(task1);
        Future<Integer> f2 = executor.submit(task2);

        System.out.println("task1 is compeleted ?" + f1.isDone());
        System.out.println("task2 is compeleted ?" + f2.isDone());


        while (f1.isDone())
        {
            System.out.println("task1 has compeleted");
            break;
        }

        while (f2.isDone())
        {
            System.out.println("return value by task2:"+ f2.get());
            break;
        }



    }

}
